#pragma  once
#include "MenyScene.h"
#include <QLabel>
#include <QDebug>
#include <QColor>

MenyScene::MenyScene(int id, QRect &r): QGraphicsScene()
{
    items=0;
    switch(id)
    {
    case 0: //Главное меню
    {
        int width = 120; //ширина кнопки
        int deltaX = (r.width()/2-width); //позиция отрисовки на экране
        int deltaY = r.height()/2+200; // смещение по вертикали
        int height = 20; // высота кнопки
        int posY =deltaY; // динамичный сдвиг для кнопок

        BacgroundImage *im = new BacgroundImage(":/backgroundImmage/mainbackgr.jpg");
        im->scale(r.width()/im->size().width()- 0.002, r.height()/im->size().height()-0.002);
        im->setZValue(-1);
        im->setPos(0,0);
        addItem(im);

        //setBackgroundBrush(QBrush(QColor.black()));

        QLineEdit * edit = new QLineEdit();
        Edits.push_back(edit);
        QGraphicsProxyWidget * item;
        item = addWidget(edit);
        edit->insert("asda");
        QTransform transform = item->transform();
        transform.translate(40,50);
        transform.scale(2, 2);
        item->setTransform(transform);
        edit->show();
        //connect(this, SIGNAL(signalLineEdit(event)), edit, SLOT());

        QLabel * label = new QLabel(trUtf8("Введите ник:"));
        label->setStyleSheet("background-color: rgba(0,0,0,0%)");
        Labels.push_back(label);
        item = addWidget(label);
        transform = item->transform();
        transform.translate(40,20);
        transform.scale(2, 2);
        item->setTransform(transform);
        label->show();

        QPushButton * button = new QPushButton(trUtf8("Развязать конфликт"), 0);
        Buttons.push_back(button);
        ++items;

        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(deltaX, posY);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();
        posY+= height*2+5;

        button = new QPushButton(trUtf8("Присоединиться"), 0);
        Buttons.push_back(button);
        ++items;
        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(deltaX, posY);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();

        posY+= height*2+5;
        button = new QPushButton(trUtf8("Опции"), 0);
        Buttons.push_back(button);
        ++items;
        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(deltaX, posY);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();

        posY+= height*2+20;
        button = new QPushButton(trUtf8("Выход"), 0);
        Buttons.push_back(button);
        ++items;
        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(deltaX,posY);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();

        break;
    }

    case 1: // меню сервера
    {
        int width = 120; //ширина кнопки   
        int height = 20; // высота кнопки

        BacgroundImage *im = new BacgroundImage(":/backgroundImmage/GameMode.jpg");
        im->scale(r.width()/im->size().width()- 0.002, r.height()/im->size().height()-0.002);
        im->setZValue(-1);
        im->setPos(0,0);
        addItem(im);

        QGraphicsProxyWidget * item;
        QTransform transform;

        QLabel * label = new QLabel(trUtf8("Игроки:"));
        label->setStyleSheet("background-color: rgba(0,0,0,0%)");
        Labels.push_back(label);
        item = addWidget(label);
        transform = item->transform();
        transform.translate(40,100);
        transform.scale(2, 2);
        item->setTransform(transform);
        label->show();

        label = new QLabel(trUtf8(""));
        label->setFixedWidth(200);
        label->setFixedHeight(400);
        label->setObjectName("PlayersList");
        label->setStyleSheet("background-color: rgba(0,0,0,0%)");
        Labels.push_back(label);
        item = addWidget(label);
        transform = item->transform();
        transform.translate(80,130);
        transform.scale(2, 2);
        item->setTransform(transform);
        label->show();

        QPushButton * button = new QPushButton(trUtf8("Вернуться"), 0);
        Buttons.push_back(button);
        ++items;
        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(40, r.height()-60);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();

        button = new QPushButton(trUtf8("Старт"), 0);
        Buttons.push_back(button);
        ++items;
        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(r.width()-40-width*2, r.height()-60);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();
        break;
    }
    case 2: // меню клиента
    {
        int width = 120; //ширина кнопки
        int height = 20; // высота кнопки

        BacgroundImage *im = new BacgroundImage(":/backgroundImmage/GameMode.jpg");
        im->scale(r.width()/im->size().width()- 0.002, r.height()/im->size().height()-0.002);
        im->setZValue(-1);
        im->setPos(0,0);
        addItem(im);


        QLineEdit * edit = new QLineEdit();
        Edits.push_back(edit);
        QGraphicsProxyWidget * item;
        item = addWidget(edit);
        edit->setFixedWidth(100);
        edit->insert("127.0.0.1");
        QTransform transform = item->transform();
        transform.translate(40,50);
        transform.scale(2, 2);
        item->setTransform(transform);
        edit->show();

        edit = new QLineEdit();
        Edits.push_back(edit);
        edit->setFixedWidth(100);
        item = addWidget(edit);
        edit->insert("7777");
        transform = item->transform();
        transform.translate(350,50);
        transform.scale(2, 2);
        item->setTransform(transform);
        edit->show();

        edit = new QLineEdit();
        Edits.push_back(edit);
        edit->setFixedWidth(100);
        item = addWidget(edit);
        edit->insert("kamba");
        transform = item->transform();
        transform.translate(700,50);
        transform.scale(2, 2);
        item->setTransform(transform);
        edit->show();

        QLabel *label = new QLabel(trUtf8("Игроки:"));

        label->setStyleSheet("background-color: rgba(0,0,0,0%)");
        Labels.push_back(label);
        item = addWidget(label);
        transform = item->transform();
        transform.translate(40,100);
        transform.scale(2, 2);
        item->setTransform(transform);
        label->show();

        label = new QLabel(trUtf8(""));
        label->setFixedWidth(200);
        label->setFixedHeight(400);
        label->setObjectName("PlayersList");
        label->setStyleSheet("background-color: rgba(0,0,0,0%)");
        Labels.push_back(label);
        item = addWidget(label);
        transform = item->transform();
        transform.translate(80,130);
        transform.scale(2, 2);
        item->setTransform(transform);
        label->show();

        QPushButton * button = new QPushButton(trUtf8("Вернуться"), 0);
        Buttons.push_back(button);
        ++items;

        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(40, r.height()-60);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();

        button = new QPushButton(trUtf8("Старт"), 0);
        Buttons.push_back(button);
        ++items;
        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(r.width()-40-width*2, r.height()-60);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();

        button = new QPushButton(trUtf8("Присоединиться"), 0);
        Buttons.push_back(button);
        ++items;
        button->setFixedWidth(width);
        button->setFixedHeight(height);
        item = addWidget(button);
        transform = item->transform();
        transform.translate(r.width()-40-width*2, 50);
        transform.scale(2, 2);
        item->setTransform(transform);
        button->show();
        break;
    }
    case 3: //игровое меню.

        break;
    case 4:
    {

    }
    }

}

MenyScene::~MenyScene()
{
    for(int i = 0; i < Buttons.size();++i)
    {
        delete Buttons[i];
    }

    for(int i = 0; i < Edits.size();++i)
    {
        delete Edits[i];
    }

    for(int i = 0; i < Labels.size();++i)
    {
        delete Labels[i];
    }
}
int MenyScene::GetCountItems()
{
    return items;
}

QPushButton* MenyScene::GetButtom(int id)
{
    if(id < Buttons.size())
    {
       return Buttons[id];
    }
    return 0;
}
QLineEdit* MenyScene::GetEdit(int id)
{
    if(id < Edits.size())
    {
       return Edits[id];
    }
    return 0;
}

QLabel* MenyScene::GetLabel(int id)
{
    if(id < Labels.size())
    {
       return Labels[id];
    }
    return 0;
}

int MenyScene::GetLabelCount()
{
   return Labels.size();
}


QLabel* MenyScene::GetPlayerListLabel()
{
    foreach (QLabel *l, Labels)
    {
        if(l->objectName()=="PlayersList")
        {
            return l;
        }
    }
    return NULL;
}

void MenyScene::keyPressEvent(QKeyEvent *event)
{
    qDebug() << "asda";
    if (event->key() == Qt::Key_Escape)
    {
        emit signalPresEscKey();
        return;
    }

    for (int i = 0; i < Edits.size(); ++i )
    {
        if ( GetEdit(i)->hasFocus ())
        {
            GetEdit(i)->event(event);
            return;
        }
    }
    emit signalPresKey(event);
}
